﻿#include "Manager.h"

// 默认构造
Manager::Manager()
{
}

// 有参构造
Manager::Manager(string name, string pwd, ComputerRoomFile* c)
{
	this->m_Name = name;
	this->m_Pwd = pwd;
	this->m_ComputerRoomFile = c;
	this->initVector();// 初始化容器
}

// 默认析构
Manager::~Manager()
{
}

// 管理员菜单界面
void Manager::operMenu()
{
	cout << "欢迎管理员：" << this->m_Name << "登录！" << endl;
	cout << "\t\t ---------------------------------\n";
	cout << "\t\t|                                |\n";
	cout << "\t\t|          1.添加账号            |\n";
	cout << "\t\t|                                |\n";
	cout << "\t\t|          2.查看账号            |\n";
	cout << "\t\t|                                |\n";
	cout << "\t\t|          3.查看机房            |\n";
	cout << "\t\t|                                |\n";
	cout << "\t\t|          4.清空预约            |\n";
	cout << "\t\t|                                |\n";
	cout << "\t\t|          0.注销登录            |\n";
	cout << "\t\t|                                |\n";
	cout << "\t\t ---------------------------------\n";
	cout << "请选择您的操作： " << endl;
}

// 添加账号
void Manager::addPerson()
{
	int identity;
	cout << "请输入需要添加账号的身份：" << endl;
	cout << "1-学生 2-老师" << endl;
	cin >> identity;

	// 添加账号实际上就是写入到文件中
	string fileName;
	string tip;
	string wrongTip;
	if (identity == 1)
	{
		fileName = STUDENT_FILE;
		tip = "请输入学号：";
		wrongTip = "学号重复！";
	}
	else
	{
		fileName = TEACHER_FILE;
		tip = "请输入老师编号：";
		wrongTip = "老师编号重复！";
	}
	ofstream ofs;
	ofs.open(fileName, ios::out | ios::app);

	int id;
	string name;
	string pwd;
	while (true)
	{
		cout << tip << endl;
		cin >> id;
		if (!checkRepeat(id, identity))
		{
			break;
		}
		else
		{
			cout << wrongTip << endl;
		}
	}

	cout << "请输入用户名：" << endl;
	cin >> name;
	cout << "请输入密码：" << endl;
	cin >> pwd;

	ofs << id << " " << name << " " << pwd << " " << endl;
	ofs.close();
	cout << "添加成功！" << endl;

	//this->initVector();// 更新数据，重新从文件读
	if (identity == 1)
	{
		Student s(id, name, pwd);
		this->m_vStu.push_back(s);
	}
	else
	{
		Teacher t(id, name, pwd);
		this->m_vTea.push_back(t);
	}
	system("pause");
	system("cls");
}

// 展示账号信息
void Manager::showPerson()
{
	cout << "请选择查看内容：" << endl;
	cout << "1-查看所有学生" << endl;
	cout << "2-查看所有老师" << endl;

	int select;
	cin >> select;
	if (select == 1)
	{
		cout << "所有学生的信息如下：" << endl;
		for_each(m_vStu.begin(), m_vStu.end(), [](const Student& s) {
			cout << "学号：" << s.m_Id << " 姓名：" << s.m_Name << " 密码：" << s.m_Pwd << endl;
			});
	}
	else
	{
		cout << "所有老师的信息如下：" << endl;
		for_each(m_vTea.begin(), m_vTea.end(), [](const Teacher& t) {
			cout << "老师编号：" << t.m_Id << " 姓名：" << t.m_Name << " 密码：" << t.m_Pwd << endl;
			});
	}
	system("pause");
	system("cls");
}

// 展示机房信息
void Manager::showComputerRoom()
{
	cout << "所有机房的信息如下：" << endl;
	/*for_each(m_vCom.begin(), m_vCom.end(), [](const ComputerRoom& com) {
		cout << "机房号：" << com.m_Id << " 最大容量：" << com.m_MaxNum << endl;
		});*/
	this->m_ComputerRoomFile->showInfo();
	system("pause");
	system("cls");
}

// 清空预约记录
void Manager::cleanFile()
{
	int select;
	cout << "是否真的清空所有预约记录？" << endl;
	cout << "1-是 2-否" << endl;
	cin >> select;
	if (select == 1)
	{
		ofstream ofs;
		ofs.open(ORDER_FILE, ios::trunc);
		ofs.close();
		cout << "清空成功！" << endl;
	}

	this->m_ComputerRoomFile->resetComputerRoomData();
	system("pause");
	system("cls");
}

// 初始化容器
void Manager::initVector()
{
	// 先把容器都置空
	this->m_vStu.clear();
	this->m_vTea.clear();
	//this->m_vCom.clear();
	//加载学生文件信息
	ifstream ifs;
	ifs.open(STUDENT_FILE, ios::in);
	if (!ifs.is_open())
	{
		cout << "学生文件不存在！" << endl;
		ifs.close();
		return;
	}
	Student s;
	while (ifs >> s.m_Id && ifs >> s.m_Name && ifs >> s.m_Pwd)
	{
		this->m_vStu.push_back(s);
	}
	//cout << "学生的数量为：" << this->m_vStu.size() << endl;
	ifs.close();

	//加载老师文件信息
	ifs.open(TEACHER_FILE, ios::in);
	if (!ifs.is_open())
	{
		cout << "老师文件不存在！" << endl;
		ifs.close();
		return;
	}
	Teacher t;
	while (ifs >> t.m_Id && ifs >> t.m_Name && ifs >> t.m_Pwd)
	{
		this->m_vTea.push_back(t);
	}
	//cout << "老师的数量为：" << this->m_vTea.size() << endl;
	ifs.close();
}

// 检测id是否重复
bool Manager::checkRepeat(int id, int identity)
{
	if (identity == 1)
	{
		for (vector<Student>::iterator it = this->m_vStu.begin(); it != this->m_vStu.end(); it++)
		{
			if (id == it->m_Id)
			{
				return true;
			}
		}
		// find_if写法，麻烦，不推荐
		/*vector<Student>::iterator it = find_if(this->m_vStu.begin(), this->m_vStu.end(), [=](const Student& s) {
			return id == s.m_Id;
		});
		if (it != this->m_vStu.end())
		{
			return true;
		}*/
	}
	else
	{
		for (vector<Teacher>::iterator it = this->m_vTea.begin(); it != this->m_vTea.end(); it++)
		{
			if (id == it->m_Id)
			{
				return true;
			}
		}
	}
	return false;
}